package de.wbh.gwt.form.layouter.gwt.ui.view;

import javax.inject.Inject;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckLayoutPanel;
import com.google.gwt.user.client.ui.HasOneWidget;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;

import de.wbh.gwt.form.layouter.shared.SkeletonView;

public class SkeletonViewImpl extends Composite implements RequiresResize, SkeletonView {

	public static interface Resources extends ClientBundle {
		@ImageOptions(height = 60, width = 221)
		ImageResource logo();
	}

	interface SkeletonViewImplUiBinder extends UiBinder<DeckLayoutPanel, SkeletonViewImpl> {
	}

	private static SkeletonViewImplUiBinder uiBinder = GWT.create(SkeletonViewImplUiBinder.class);

	@UiField
	SimpleLayoutPanel deck1Center;

	@UiField
	SimpleLayoutPanel deck2Top;

	@UiField
	SimpleLayoutPanel deck2Left;

	@UiField
	DeckLayoutPanel deck2Right;

	@UiField
	LayoutPanel stack1;

	@UiField
	LayoutPanel stack2;

	@UiField
	AbsolutePanel dragArea;

	private final DeckLayoutPanel panel;

	@UiField(provided = true)
	Resources resources;

	@Inject
	public SkeletonViewImpl(Resources resources) {
		this.resources = resources;
		initWidget(this.panel = uiBinder.createAndBindUi(this));
		this.panel.showWidget(0);
	}

	@Override
	public void expandLeft() {
		this.stack2.setWidgetLeftWidth(this.deck2Left, 0, Unit.PX, 480, Unit.PX);
		this.stack2.setWidgetLeftRight(this.deck2Right, 350, Unit.PX, 0, Unit.PX);
		this.stack2.animate(1000);
	}

	@Override
	public HasOneWidget getDeck1Center() {
		return this.deck1Center;
	}

	@Override
	public HasOneWidget getDeck2Left() {
		return this.deck2Left;
	}

	@Override
	public AcceptsOneWidget getDeck2Right() {
		return this.deck2Right;
	}

	@Override
	public AbsolutePanel getDragArea() {
		return this.dragArea;
	}

	@Override
	protected void onAttach() {
		super.onAttach();
		Scheduler.get().scheduleDeferred(new Command() {

			@Override
			public void execute() {
				onResize();
			}
		});
	}

	@Override
	public void onResize() {
		int width = getOffsetWidth();
		int left = (width - 400) / 2;
		this.stack1.setWidgetLeftWidth(this.deck1Center, left, Unit.PX, 400, Unit.PX);

		int height = getOffsetHeight();
		int top = (height - 300) / 2;
		this.stack1.setWidgetTopHeight(this.deck1Center, top, Unit.PX, 300, Unit.PX);
		this.stack1.forceLayout();
	}

	@Override
	public void showStartDeck(boolean showStartDeck) {
		if (showStartDeck) {
			this.panel.showWidget(0);
		} else {
			System.out.println("showWidget 1");
			this.panel.showWidget(1);
		}
	}

	@Override
	public void shrinkLeft() {
		this.stack2.setWidgetLeftWidth(this.deck2Left, 0, Unit.PX, 330, Unit.PX);
		this.stack2.setWidgetLeftRight(this.deck2Right, 200, Unit.PX, 0, Unit.PX);
		this.stack2.animate(1000);
	}
}
